import { _decorator, Component, easing, Node, Tween, tween, UIOpacity, Vec3 } from "cc";
import { DialogViewID } from "../../components/DialogViewID";
import { Handler } from "../../components/Handler";
import { DialogViewManager } from "../../managers/DialogViewManager";
import { ILanguage, LanguageMgr } from "../../components/language/LanguageMgr";
import { GameDataMgr } from "../../managers/GameDataMgr";
import { GlobalEventsMgr } from "../../managers/GlobelEventMgr";
const { ccclass, property } = _decorator;
@ccclass('BaseDialog')
export class DialogViewBase extends Component  implements ILanguage{
        /**主界面*/
    @property(Node)
    protected content: Node;
       /**背景遮罩*/
    @property(Node)
    protected bgMask:Node;
    protected _dialogViewID:DialogViewID;
    private _handlerMap:Map<Function,Handler<any>> = new Map();
    private _moveTween:any;
    private _bgMoveTween:any;
    private _data:any;
    protected start(): void {
        LanguageMgr.instance.addILanguage(this);
        this.refreshLanguage();
        this.onStart();
    }
    public inspectID(dialogViewID:string):void{
        if(this._dialogViewID!=dialogViewID)
        {
            console.error(`窗口ID与注册ID不一致注册ID：${dialogViewID}，本地设置id:${this._dialogViewID}`);
        }
    }
    /**预制体加载完毕 这个方法会被调用并把open带的自定义数据带进来，但窗口被重复open这个方法也会多次触发*/
    public setData(data?):void{ 
        this._data = data;
    }
    /**子类重写这个方法来代替start方法*/
    protected onStart():void{

    }
    public show():void{
        if(this.content)
        {
            this.content.scale = new Vec3(0,0,0)
            if(this._moveTween)
                {
                    this._moveTween.stop();
                }
            this._moveTween = tween(this.content)
            .to(0.3,{scale:new Vec3(1,1,1)},{ easing:easing.circOut})
            .call(() => {
            })
            .start();
        }
        if (this.bgMask) {
            if (this._bgMoveTween) {
                this._bgMoveTween.stop();
            }
            if(!this.bgMask.getComponent(UIOpacity))
            {
                this.bgMask.addComponent(UIOpacity);
            }
            let c = this.bgMask.getComponent(UIOpacity);
            // 设置初始透明度为0
            c.opacity = 100;
            // 创建背景遮罩的缓动动画，将透明度从0变为1
            this._bgMoveTween = tween(c)
                .to(1, { opacity: 255 })  // 255 表示完全不透明，这里的取值范围根据你的需求来调整
                .start();
        }
    }
    public getDialogViewID():DialogViewID{
        return this._dialogViewID;
    }
    public getHandler(callback:Function):Handler<any>{
        let h:Handler<any> = new Handler(this,callback);
        this._handlerMap.set(callback,h);
        return h;
    }
    public removeHandler(callback:Function){
        let h:Handler<any> = this._handlerMap.get(callback);
        if(h)
        {
            h.clear();
            this._handlerMap.delete(callback);
        }
    }
    public close(){
        DialogViewManager.instance.closeDialog(this.getDialogViewID());
    }
    /**切换语言时这个函数会触发*/
    refreshLanguage() {
        
    }
    protected onDestroy(): void {
        if(this._bgMoveTween)
        {
            this._bgMoveTween.stop();
        }
        this._handlerMap.forEach((v,k)=>{
            v.clear();
        })
        this._handlerMap.clear();
        GlobalEventsMgr.instance.offAll(this);
        GameDataMgr.instance.offAll(this);
        LanguageMgr.instance.removeILanguage(this)
       this.close();
    }
}